package me.xplabs.engine.time 
{
	/**
	 * ...
	 * @author xiaohan
	 */
	public class TimeGapNode 
	{
		public var gap:int;
		public var func:Function;
		public var endFunc:Function;
		public var key:int;
		
		private var _last:int;
		private var _isRepect:Boolean;
		private var _repect:int;
		private var _per:int;
		private var _isJumpPer:Boolean;
		public function TimeGapNode(pkey:int, pnow:int, pgap:Number, pfunc:Function, pendFunc:Function=null, ptime:int = -1, pisJumpPer:Boolean = true)
		{
			key = pkey;
			gap = pgap * 1000;
			func = pfunc;
			endFunc = pendFunc;
			_isRepect = ptime == -1;
			_repect = _isRepect? 1:ptime-1;
			_last = pnow + gap;
			_isJumpPer = pisJumpPer;
		}
		
		public function dispose():void
		{
			if (endFunc != null) endFunc(key);
		}
		
		public function update(p:int):Boolean
		{
			if (p < _last) return false;
			if (func != null)
			{
				if (func.length == 0) func();
				else if (func.length == 1) func(_repect);
				else if (func.length == 2) func(_repect, key);
			}
			if (_isRepect || _repect > 0) {
				_per = int((p - _last) / gap) + 1;
				if (!_isJumpPer) {
					for (var i:int = 0; i < _per - 1; i++) 
					{
						if (func != null)
						{
							if (func.length == 0) func();
							else if (func.length == 1) func(_repect + i);
							else if (func.length == 2) func(_repect + i, key);
						}
					}
				}
				_last += _per * gap;
				if(!_isRepect) _repect -= _per;
				return false;
			}
			return true;
		}
	}
}